package com.example.ad_project;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;

import com.nus.iss.ad.dbhelper.AuthenticateDBHelper;
import com.nus.iss.ad.dbhelper.RetrivalListDBHelper;

public class BaseActivity extends Activity {
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		case R.id.action_logout:
		
			logOut();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	public void logOut(){
		AuthenticateDBHelper data=new AuthenticateDBHelper();
		data.logout();
		RetrivalListDBHelper.clearJson();
		
		Intent intent=new Intent(this,MainActivity.class);
		intent.putExtra("logout", true);//Not to Auto Login
		startActivity(intent);
	}
	
	public void showDialog(String message){
		showDialog(this.getTitle().toString(),message,false);
	}
	
	public void showDialog(String message,final boolean closeActivity){
		showDialog(this.getTitle().toString(),message,closeActivity);
	}
	
	public void showDialog(String title,String message,final boolean closeActivity){
		AlertDialog.Builder b = new AlertDialog.Builder(this);
        b.setTitle(title)
         .setMessage(message)
         .setCancelable(false)
         .setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                  dialog.dismiss();
                  if(closeActivity)
                  {setResult(RESULT_OK);
                  finish();
                  }
               }
            });
         b.show();
	}
}
